/*
 * Helstius--Basic chemistry application
 * This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 * Copyright 2012 Jaked122
 */
package org.helstius.Gui;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.GlyphVector;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Arc2D.Double;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.helstius.util.Utility;

/**
 * <p>
 * Didn't feel like calling it a Pie chart, so here is a class with a
 * "Petal Diagram" instead. It works fairly well.
 * </p>
 * 
 * @author Jaked122
 * 
 */
public class PetalDiagram extends Canvas implements IChart {
	public void paint(Graphics g) {

		super.paint(g);
		if (data != null) {
			double anglestep = 360 / Utility.sum(data);
			int sumsofar = 0;
			Graphics2D t = (Graphics2D) g;
			gradient = new ColorGradient(Start, End, data.length);

			RenderingHints h, j;
			if (antialias)
				h = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
			else
				h = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_OFF);
			if (Quality)
				j = new RenderingHints(RenderingHints.KEY_RENDERING,
						RenderingHints.VALUE_RENDER_QUALITY);
			else
				j = new RenderingHints(RenderingHints.KEY_RENDERING,
						RenderingHints.VALUE_RENDER_SPEED);

			t.addRenderingHints(h);
			t.addRenderingHints(j);
			for (int i = 0; i < data.length; i++) {
				// set the color to a random thingy
				if (colormode == COLOR_GRADIENT)
					t.setColor(gradient.getAtStep(i));
				else if (colormode == COLOR_RANDOM)
					t.setColor(new Color(new Random().nextInt()));
				t.fill(new Arc2D.Double(new Rectangle2D.Double(0, 0,
						getWidth(), getHeight()), sumsofar,
						anglestep * data[i], Arc2D.CHORD));

				t.draw(new Arc2D.Double(new Rectangle2D.Double(0, 0,
						getWidth(), getHeight()), sumsofar,
						anglestep * data[i], Arc2D.PIE));

				// g.fillArc(0, 0, getWidth(), getHeight(), sumsofar, anglestep
				// * data[i]);
				sumsofar += anglestep * data[i];
			}
		}

	}

	private Color Start = Color.green, End = Color.red;

	public Color getStart() {
		return Start;
	}

	/**
	 * Set the starting color for the gradient that will be used if Colormode is
	 * set to COLOR_GRADIENT.
	 * 
	 * @param start
	 *            The color to set.
	 */
	public void setStart(Color start) {
		Start = start;
	}

	/**
	 * Turns on Quality Rendering.
	 * 
	 * @see java.awt.RenderingHints.VALUE_COLOR_RENDER_SPEED
	 */
	boolean Quality = true;

	/**
	 * Sets the Quality flag
	 * 
	 * @param q
	 *            The boolean to set it to.
	 * @see Quality
	 */
	public void setQuality(boolean q) {
		Quality = q;
	}

	public Color getEnd() {
		return End;
	}

	/**
	 * Set the ending Color of the Gradient which is used if Colormode is set to
	 * COLOR_GRADIENT;
	 * 
	 * @see COLOR_GRADIENT
	 * @param end
	 *            The Color that should be at the end.
	 */
	public void setEnd(Color end) {
		End = end;
	}

	public String getColormode() {
		return colormode;
	}

	public void setColormode(String colormode) {
		this.colormode = colormode;
	}

	public boolean isAntialias() {
		return antialias;
	}

	public void setAntialias(boolean antialias) {
		this.antialias = antialias;
	}

	public int[] getData() {
		return data;
	}

	public void setData(int[] data) {
		this.data = data;
		repaint();
	}

	public void setData(List<Integer> data) {
		this.data = new int[data.size()];
		for (int i = 0; i < data.size(); i++)
			this.data[i] = data.get(i);
		repaint();
	}

	private String colormode = COLOR_GRADIENT;
	/**
	 * <p>
	 * When the PetalDiagram is set to this mode, all data slices will be
	 * represented by random colors.
	 * </p>
	 * 
	 * @see COLOR_GRADIENT
	 */
	public static String COLOR_RANDOM = "rand";
	/**
	 * <p>
	 * When the PetalDiagram is set to this, colors will be interpolated for
	 * each value in the set of numbers.
	 * </p>
	 * 
	 * @see org.helstius.Gui.PetalDiagram.COLOR_RANDOM
	 */
	public static String COLOR_GRADIENT = "GRAD";
	private boolean antialias = true;;

	/**
	 * Add data to the chart.
	 * 
	 * @param da
	 *            the data to add.
	 */
	public void addData(int[] da) {
		if (da != null)
			data = da;
		else
			data = new int[] { 1 };
		repaint();
	}

	public void setData(Integer[] i) {
		data = new int[i.length];
		for (int c = 0; c < i.length; c++)
			data[c] = i[c].intValue();
		repaint();
	}

	public void addData(List<Integer> da) {
		if (da != null) {
			data = new int[da.size()];
			for (int i = 0; i < da.size(); i++)
				data[i] = da.get(i);
		} else
			data = new int[] { 1 };
		repaint();
	}

	private int[] data;
	private ColorGradient gradient;

	public ColorGradient getgradient() {
		return gradient;
	}

	@Override
	public void addNames(String[] names) {
		// TODO Auto-generated method stub
		this.names = names;
	}

	String[] names;

	@Override
	public String[] getNames() {
		// TODO Auto-generated method stub
		return names;
	}

	@Override
	public Color getElementColor(int element) {
		// TODO Auto-generated method stub
		return gradient.getAtStep(element);
	}
}
